package com.ruoyi.web.controller.system;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GroupChatMessage;
import com.ruoyi.system.service.IGroupChatMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 群聊消息记录Controller
 * 
 * @author ruoyi
 * @date 2025-11-02
 */
@RestController
@RequestMapping("/system/message")
public class GroupChatMessageController extends BaseController
{
    @Autowired
    private IGroupChatMessageService groupChatMessageService;



    // 拉取群聊历史消息
    @GetMapping("/api/chat/history")
    public List<GroupChatMessage> getHistoryMessages(@RequestParam String groupId) {
/*        QueryWrapper<GroupChatMessage> query = new QueryWrapper<>();
        query.eq("group_id", groupId).orderByAsc("send_time");
        GroupChatMessage groupChatMessage = new GroupChatMessage();*/
        GroupChatMessage groupChatMessage = new GroupChatMessage();
        groupChatMessage.setGroupId(groupId);
        return groupChatMessageService.getHistoryMessages(groupChatMessage);
    }
    /**
     * 查询群聊消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(GroupChatMessage groupChatMessage)
    {
        startPage();
        List<GroupChatMessage> list = groupChatMessageService.selectGroupChatMessageList(groupChatMessage);
        return getDataTable(list);
    }

    /**
     * 导出群聊消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "群聊消息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GroupChatMessage groupChatMessage)
    {
        List<GroupChatMessage> list = groupChatMessageService.selectGroupChatMessageList(groupChatMessage);
        ExcelUtil<GroupChatMessage> util = new ExcelUtil<GroupChatMessage>(GroupChatMessage.class);
        util.exportExcel(response, list, "群聊消息记录数据");
    }

    /**
     * 获取群聊消息记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(groupChatMessageService.selectGroupChatMessageById(id));
    }

    /**
     * 新增群聊消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "群聊消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GroupChatMessage groupChatMessage)
    {
        return toAjax(groupChatMessageService.insertGroupChatMessage(groupChatMessage));
    }

    /**
     * 修改群聊消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "群聊消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GroupChatMessage groupChatMessage)
    {
        return toAjax(groupChatMessageService.updateGroupChatMessage(groupChatMessage));
    }

    /**
     * 删除群聊消息记录
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "群聊消息记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(groupChatMessageService.deleteGroupChatMessageByIds(ids));
    }
}
